import settings from "../utils/settings";
import { getPlayerPos } from "../utils/utils";
import { Vec3 } from "../utils/vec3";

/**
 * 将一个坐标按照角度进行旋转
 * @param {Vec3} loc 相对位置
 * @param {number} yaw 偏航角
 */
function rotation(loc, yaw = Player.getYaw()) {
    let radian = yaw * Math.PI / 180;
    let x = loc.x * Math.cos(radian) - loc.z * Math.sin(radian);
    let z = loc.x * Math.sin(radian) + loc.z * Math.cos(radian);
    return new Vec3(x, loc.y, z);
}

/**
 * 在玩家周围生成粒子
 * @param {string} name 粒子名字
 * @param {number} x 相对玩家的x坐标
 * @param {number} y 相对玩家的y坐标
 * @param {number} z 相对玩家的z坐标
 * @param {number} sx 粒子的x轴速度
 * @param {number} sy 粒子的y轴速度
 * @param {number} sz 粒子的z轴速度
 */
function spawnParticleOnPlayer(name, x, y, z, sx = 0, sy = 0, sz = 0) {
    let loc = rotation(new Vec3(x, y, z));
    let pos = getPlayerPos();
    pos = pos.add(loc);
    World.particle.spawnParticle(name,pos.x, pos.y, pos.z, sx, sy, sz);
}


let isStanding = false;
let lastPos = new Vec3(0, 0, 0);
register("step", () => {
    if(!Player.getPlayer()) return;
    let playerPos = getPlayerPos();
    if(lastPos.x == playerPos.x && lastPos.y == playerPos.y && lastPos.z == playerPos.z) isStanding = true; else isStanding = false;
    lastPos = getPlayerPos();
}).setDelay(1);

/*
全粒子名称
EXPLOSION_NORMAL
EXPLOSION_LARGE
EXPLOSION_HUGE
FIREWORKS_SPARK
WATER_BUBBLE
WATER_SPLASH
WATER_WAKE
SUSPENDED
SUSPENDED_DEPTH
CRIT
CRIT_MAGIC
SMOKE_NORMAL
SMOKE_LARGE
SPELL
SPELL_INSTANT
SPELL_MOB
SPELL_MOB_AMBIENT
SPELL_WITCH
DRIP_WATER
DRIP_LAVA
VILLAGER_ANGRY
VILLAGER_HAPPY
TOWN_AURA
NOTE
PORTAL
ENCHANTMENT_TABLE
FLAME
LAVA
FOOTSTEP
CLOUD
REDSTONE
SNOWBALL
SNOW_SHOVEL
SLIME
HEART
BARRIER
ITEM_CRACK
BLOCK_CRACK
BLOCK_DUST
WATER_DROP
ITEM_TAKE
MOB_APPEARANCE
*/

export function loadWings() {
    register("step", () => {
        if(settings.wings == 0) return;
        if(!isStanding) return;
        showWings();
        setTimeout(() => { showWings(); }, 500);
    }).setDelay(1);
}

function showWings() {
    switch (settings.wings) {
        case 1:
            showColorWing("FLAME");
            break;
        case 2:
            showColorWing("REDSTONE");
            break;
        case 3:
            showColorWing("ENCHANTMENT_TABLE");
            break;
        case 4:
            showColorWing("SMOKE_NORMAL");
            break;
        case 5:
            showColorWing("PORTAL");
            break;
    }
}

function showColorWing(particleName) {
    for (let x = -0.5 , y = 0.5; x <= 1.1; x+=0.2 , y+=0.2) {
        spawnParticleOnPlayer(particleName, x-0.2, y, -0.5);
        spawnParticleOnPlayer(particleName, x, y, -0.5);
        spawnParticleOnPlayer(particleName, x+0.2, y, -0.5);
    }
    for (let x = 0.5 , y = 0.5; x >= -1.1; x-=0.2 , y+=0.2) {
        spawnParticleOnPlayer(particleName, x-0.2, y, -0.5);
        spawnParticleOnPlayer(particleName, x, y, -0.5);
        spawnParticleOnPlayer(particleName, x+0.2, y, -0.5);
    }
}